package com.publicize.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.publicize.commom.commonutils.JwtUtils;
import com.publicize.commom.commonutils.Result;
import com.publicize.entity.Module;
import com.publicize.entity.ModulePicture;
import com.publicize.entity.Relationship;
import com.publicize.service.ModuleService;
import com.publicize.service.RelationshipService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
/**
 * <p>
 * 投稿信息表 前端控制器
 * </p>
 *
 * @author bs
 * @since 2021-03-04
 */
@RestController
@RequestMapping("/proscenium/module")
@CrossOrigin
public class ModuleController {
    @Resource
    private ModuleService moduleService;
    @Resource
    private RelationshipService relationshipService;

    //查看所有模块
    @GetMapping("selectRelationship")
    public Result selectRelationship(){
        List<Relationship> list = relationshipService.list(null);
        return Result.ok().data("relationships",list);
    }

    //根据模块id查看对应稿子
    @GetMapping("selectModuleById/{moduleId}/{page}/{limit}")
    public Result  selectModuleById(@PathVariable("moduleId") String moduleId,
                                    @PathVariable("page") Integer page,
                                    @PathVariable("limit") Integer limit){
        List<Module> list=moduleService.selectModuleById(moduleId,page,limit);
        Integer count=moduleService.countModuleByRelationId(moduleId);

        return Result.ok().data("modules",list).data("total",count);
    }

    //根据用户id查看对应稿子
    @PostMapping("selectModuleByUserId")
    public  Result  selectModuleByUserId(@RequestParam String userId,@RequestParam String relationId){
        List<Module> list=moduleService.selectModuleByUserIdAndRelationId(userId,relationId);
        QueryWrapper<Module> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("user_id",userId);
        queryWrapper.eq("relationship_id",relationId);

        return Result.ok().data("modules",list).data("total",moduleService.count(queryWrapper));
    }

    //用户投稿
    @PostMapping("addModule")
    public Result addModule(@RequestBody Module module, HttpServletRequest request){
        String userId = JwtUtils.getMemberIdByJwtToken(request);
        module.setUserId(userId);

        boolean save = moduleService.saveModule(module);
        return save==true?Result.ok().data("module",module):Result.error();
    }
    //删除稿子
    @DeleteMapping("deleteModule/{number}")
    public Result deleteModule(@PathVariable("number") String number){
        boolean flag=moduleService.deleteModule(number);
        return flag==true?Result.ok():Result.error();
    }
}

